package csci5115.autoalarm;

import java.util.Date;

import android.content.ContentValues;
import android.database.Cursor;

public class AAEvent {
	
	public String mTitle, mAlarmType;
	String mLocation;
	Date mDate;
	byte mTransportation;
	Boolean mAlarmStatus;
	int mId;
	
	public AAEvent(String Title,
			Date Date, Boolean AlarmStatus, 
			String AlarmType, byte Transportation, String location) {
		mTitle = Title;
		mDate = Date;
		mAlarmStatus = AlarmStatus;
		mAlarmType = AlarmType;
		mTransportation = Transportation;
		mLocation = location;
	}
	
	public AAEvent(int id, String Title,
			Date Date, Boolean AlarmStatus, 
			String AlarmType, byte Transportation, String location) {
		mId = id;
		mTitle = Title;
		mDate = Date;
		mAlarmStatus = AlarmStatus;
		mAlarmType = AlarmType;
		mTransportation = Transportation;
		mLocation = location;
	}
	
	//get event from database cursor
	public AAEvent(Cursor c) {
		mId = c.getInt(0);
		mTitle = c.getString(1);
		mDate = new Date(c.getLong(2));
		int as = c.getInt(3);
		if(as == 0){
			mAlarmStatus = false;
		} else {
			mAlarmStatus = true;
		}
		mAlarmType = c.getString(4);
		int tr = c.getInt(7);
		
		mTransportation = (byte) tr;
		mLocation = c.getString(8);
	}
	
	public ContentValues toDB() {
		ContentValues cv = new ContentValues();
		cv.put(EventDBAdapter.KEY_TITLE, mTitle);
		cv.put(EventDBAdapter.KEY_DATE, mDate.getTime());
		cv.put(EventDBAdapter.KEY_DAY, mDate.getDate());
		cv.put(EventDBAdapter.KEY_MONTH, mDate.getMonth() + 1);
		cv.put(EventDBAdapter.KEY_ALARM_STATUS, mAlarmStatus ? 0:1);
		cv.put(EventDBAdapter.KEY_ALARM_TYPE, mAlarmType);
		cv.put(EventDBAdapter.KEY_TRANSPORTATION, mTransportation);
		cv.put(EventDBAdapter.KEY_LOCATION, mLocation.toString());
		
		return cv;
	}
	
	public ContentValues replaceDB() {
		ContentValues cv = new ContentValues();
		cv.put(EventDBAdapter.KEY_ID, mId);
		cv.put(EventDBAdapter.KEY_TITLE, mTitle);
		cv.put(EventDBAdapter.KEY_DATE, mDate.getTime());
		cv.put(EventDBAdapter.KEY_DAY, mDate.getDate());
		cv.put(EventDBAdapter.KEY_MONTH, mDate.getMonth() + 1);
		cv.put(EventDBAdapter.KEY_ALARM_STATUS, mAlarmStatus ? 0:1);
		cv.put(EventDBAdapter.KEY_ALARM_TYPE, mAlarmType);
		cv.put(EventDBAdapter.KEY_TRANSPORTATION, mTransportation);
		cv.put(EventDBAdapter.KEY_LOCATION, mLocation.toString());
		
		return cv;
	}
	
	public String getTitle() {
		return mTitle;
	}
	
	public String getDay() {
		String format = mDate.getMonth() + "/" + mDate.getDate();
		return format;
	}
	
	public String getTime() {
		int month = mDate.getMonth() + 1;
		int day = mDate.getDate();
		int year = mDate.getYear() + 1900;
		
		String ampm = "am";
		int hours = mDate.getHours();
		if (hours == 0) {
			hours = 12;
		} else if (hours > 12) {
			hours -= 12;
			ampm = "pm";
		}
		
		String minutes;
		int min = mDate.getMinutes();
		if(min < 10) {
			minutes = "0" + min;
		} else {
			minutes = "" + min;
		}
		String format = month + "/" + day + "/" + year + "   " + hours + ":" + minutes + ampm;
		return format;
	}
	
	public long getDateLong() {
		return mDate.getTime();
	}
	
	public void setID(int i) {
		mId = i;
	}
	
	public int getID() {
		return mId;
	}
	
	public boolean getAlarm() {
		return mAlarmStatus;
	}
	
	public String getLocation() {
		return mLocation;
	}
	
}
